/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mobilottery;

import XmlUtils.kXMLElement;
import java.util.Vector;

/**
 *
 * @author samiem
 */
public class XmlManager
{

    public static class LoginDetails
    {

        public int Status = -1;
        public String StatusDesc = null;
        //public String RecentlyFinishedGames = null;
        public String LoginDate = null;

        public boolean loginSuccessfuly()
        {
            return Status == 1;
        }
    }

    public static LoginDetails ProcessXml_Login(String p_xml)
    {
        LoginDetails loginDetails = new LoginDetails();

        if (p_xml == null)
        {
            loginDetails.Status = -1;
            loginDetails.StatusDesc = "no login details";
        }
        else
        {
            try
            {
                kXMLElement xmlParser = new kXMLElement();
                xmlParser.parseString(p_xml);
                Vector childNodes = xmlParser.getChildren();

                // Release resources
                p_xml = null;
                xmlParser = null;

                String nodeName = null;
                kXMLElement node = null;
                int size = childNodes.size();

                // Skip to status node
                for (int i = 0; i < size; i++)
                {
                    node = (kXMLElement) childNodes.elementAt(i);
                    nodeName = node.getTagName().toLowerCase();
                    if (nodeName.equals("status"))
                    {
                        loginDetails.Status = Integer.parseInt(node.getContents());
                    }
                    else if (nodeName.equals("status_desc"))
                    {
                        loginDetails.StatusDesc = node.getContents();
                    } //else if (nodeName.equals("recently_finished_games"))
                    //{
                    //    loginDetails.RecentlyFinishedGames = node.getContents();
                    //}
                    else if (nodeName.equals("login_date"))
                    {
                        loginDetails.LoginDate = node.getContents();
                    }
                }
            }
            catch (Exception ex)
            {
                loginDetails.Status = -1;
                loginDetails.StatusDesc = "login parsing error";
            }
        }
        return loginDetails;
    }

    // ------------------------------------------------------------
    //
    public static class OpenGamesDetails
    {

        public int Status = -1;
        public String StatusDesc = null;
        public String[] GamesNames = null;
        public int[] GamesIDs = null;
        public float[] GamesPrices = null;

        public boolean dataCorrect()
        {
            return Status == 1;
        }
    }
    //

    /***
     *
     */
    public static OpenGamesDetails ProcessXml_OpenGames(String p_xml)
    {
        OpenGamesDetails openGamesDetails = new OpenGamesDetails();
        
        if (p_xml == null)
        {
            openGamesDetails.Status = -1;
            openGamesDetails.StatusDesc = "new games empty list";
        }
        else
        {
            try
            {
                kXMLElement xmlParser = new kXMLElement();
                xmlParser.parseString(p_xml);
                Vector childNodes = xmlParser.getChildren();

                // Release resources
                p_xml = null;
                xmlParser = null;

                String nodeName = null;
                kXMLElement node = null;
                int size = childNodes.size();

                // Skip to status node
                for (int i = 0; i < size; i++)
                {
                    node = (kXMLElement) childNodes.elementAt(i);
                    nodeName = node.getTagName().toLowerCase();
                    if (nodeName.equals("status"))
                    {
                        openGamesDetails.Status = Integer.parseInt(node.getContents());
                    }
                    else if (nodeName.equals("status_desc"))
                    {
                        openGamesDetails.StatusDesc = node.getContents();
                    }
                    else if (nodeName.equals("games"))
                    {
                        Vector vecGames = node.getChildren();
                        int gamesCount = vecGames.size();

                        String[] gamesNames = new String[gamesCount];
                        int[] gamesIDs = new int[gamesCount];
                        float[] gamesPrices = new float[gamesCount];



                        for (int j = 0; j < vecGames.size(); j++)
                        {
                            node = (kXMLElement) vecGames.elementAt(j);

                            gamesNames[j] = node.getContents();
                            gamesIDs[j] = Integer.parseInt(node.getProperty("id", "0"));
                            gamesPrices[j] = Float.parseFloat(node.getProperty("win_price", "0"));

                        }

                        openGamesDetails.GamesNames = gamesNames;
                        openGamesDetails.GamesIDs = gamesIDs;
                        openGamesDetails.GamesPrices = gamesPrices;
                    }
                }
            }
            catch (Exception ex)
            {
                openGamesDetails.Status = -1;
                openGamesDetails.StatusDesc = "new game parsing error";
            }
        }
        return openGamesDetails;
    }

    //
    // ------------------------------------------------------------
    public static class MyGamesDetails
    {

        public int Status = -1;
        public String StatusDesc = null;
        public String[] GamesNames = null;
        public int[] GamesIDs = null;
        public long[] GamesWinningPrices = null;
        public String[] GamesDates = null;
        public String[] GamesWinningResults = null;
        public String[] GamesTransactionsIDs = null;
        public String[] GamesParticipationInfos = null;
        public float[] GamesUserWinningAmounts = null;

        public boolean dataCorrect()
        {
            return Status == 1;
        }
    }

    public static MyGamesDetails ProcessXml_MyGames(String p_xml)
    {
        MyGamesDetails myGamesDetails = new MyGamesDetails();

        if (p_xml == null)
        {
            myGamesDetails.Status = -1;
            myGamesDetails.StatusDesc = "mygames empty list";
        }
        else
        {
            try
            {
                kXMLElement xmlParser = new kXMLElement();
                xmlParser.parseString(p_xml);
                Vector childNodes = xmlParser.getChildren();

                // Release resources
                p_xml = null;
                xmlParser = null;

                String nodeName = null;
                kXMLElement node = null;
                int size = childNodes.size();

                // Skip to status node
                for (int i = 0; i < size; i++)
                {
                    node = (kXMLElement) childNodes.elementAt(i);
                    nodeName = node.getTagName().toLowerCase();
                    if (nodeName.equals("status"))
                    {
                        myGamesDetails.Status = Integer.parseInt(node.getContents());
                    }
                    else if (nodeName.equals("status_desc"))
                    {
                        myGamesDetails.StatusDesc = node.getContents();
                    }
                    else if (nodeName.equals("games"))
                    {
                        Vector vecGames = node.getChildren();
                        int gamesCount = vecGames.size();

                        String[] gamesNames = new String[gamesCount];
                        int[] gamesIDs = new int[gamesCount];
                        long[] gamesWinningPrices = new long[gamesCount];
                        String[] gamesDates = new String[gamesCount];
                        String[] gamesWinningResults = new String[gamesCount];
                        String[] gamesTransactionsIDs = new String[gamesCount];
                        String[] gamesParticipationInfos = new String[gamesCount];
                        float[] gamesUserWinningAmounts = new float[gamesCount];

                        for (int j = 0; j < vecGames.size(); j++)
                        {
                            node = (kXMLElement) vecGames.elementAt(j);

                            gamesNames[j] = node.getContents();
                            gamesIDs[j] = Integer.parseInt(node.getProperty("id", "-1"));
                            gamesWinningPrices[j] = Long.parseLong(node.getProperty("win_price", "-1"));
                            gamesDates[j] = node.getProperty("date", null);
                            gamesWinningResults[j] = node.getProperty("win_results", null);
                            gamesTransactionsIDs[j] = node.getProperty("trns_id", null);
                            gamesParticipationInfos[j] = node.getProperty("participation_info", null);
                            gamesUserWinningAmounts[j] = Float.parseFloat(node.getProperty("user_win_amount", "-1"));
                        }

                        myGamesDetails.GamesNames = gamesNames;
                        myGamesDetails.GamesIDs = gamesIDs;
                        myGamesDetails.GamesWinningPrices = gamesWinningPrices;
                        myGamesDetails.GamesDates = gamesDates;
                        myGamesDetails.GamesWinningResults = gamesWinningResults;
                        myGamesDetails.GamesTransactionsIDs = gamesTransactionsIDs;
                        myGamesDetails.GamesParticipationInfos = gamesParticipationInfos;
                        myGamesDetails.GamesUserWinningAmounts = gamesUserWinningAmounts;
                    }
                }
            }
            catch (Exception ex)
            {
                myGamesDetails.Status = -1;
                myGamesDetails.StatusDesc = "mygames parsing error";
            }
        }
        return myGamesDetails;
    }
    //
    // ------------------------------------------------------------

    public static class ParticipateInGameDetails
    {

        public int Status = -1;
        public String StatusDesc = null;
        public String TransactionId = null;

        public boolean dataCorrect()
        {
            return Status == 1;
        }
    }

    public static ParticipateInGameDetails ProcessXml_ParticipateInGame(String p_xml)
    {
        ParticipateInGameDetails gameDetails = new ParticipateInGameDetails();

        if (p_xml == null)
        {
            gameDetails.Status = -1;
            gameDetails.StatusDesc = "participate in game empty list";
        }
        else
        {
            try
            {
                kXMLElement xmlParser = new kXMLElement();
                xmlParser.parseString(p_xml);
                Vector childNodes = xmlParser.getChildren();

                // Release resources
                p_xml = null;
                xmlParser = null;

                String nodeName = null;
                kXMLElement node = null;
                int size = childNodes.size();

                // Skip to status node
                for (int i = 0; i < size; i++)
                {
                    node = (kXMLElement) childNodes.elementAt(i);
                    nodeName = node.getTagName().toLowerCase();
                    if (nodeName.equals("status"))
                    {
                        gameDetails.Status = Integer.parseInt(node.getContents());
                    }
                    else if (nodeName.equals("status_desc"))
                    {
                        gameDetails.StatusDesc = node.getContents();
                    }
                    else if (nodeName.equals("trns_id"))
                    {
                        gameDetails.TransactionId = node.getContents();
                    }
                }
            }
            catch (Exception ex)
            {
                gameDetails.Status = -1;
                gameDetails.StatusDesc = "participate in game parsing error";
            }
        }
        return gameDetails;
    }
}